﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class ArcherBullet : MonoBehaviour
{
    SpriteRenderer spriteRenderer;
    public float speed=1;
    int forward = 1;
    GameObject sender = null;
    int attack = 0;
    private void Awake()
    {
        spriteRenderer = GetComponent<SpriteRenderer>();
       
    }
    public void SetBullet(int forward,GameObject sender,int attack)
    {
        spriteRenderer.flipX = forward == 1 ? false : true;
        this.forward = forward;
        this.sender = sender;
        this.attack = attack;
        Invoke("Realse", 0.5f);
    }

    
    private void Update()
    {
        transform.Translate(transform.right*forward*Time.deltaTime*speed);
    }

    public void OnTriggerEnter2D(Collider2D collision)
    {
        if (collision.tag=="Enemy")
        {
           
            var damage = collision.transform.GetComponent<IDamage>();
            damage.OnDamage(sender,transform.position,attack);
            Realse();
        }
        else if(collision.tag=="Contact"){
             GameObjectPool.instance.CreateObject("ArcherHit",Resources.Load<GameObject>("Prefabs/Effect/AracherHit"),transform.position+Vector3.right*forward*0.1f,Quaternion.identity);
            Realse();
        }
    }
    void Realse()
    {
        sender = null;
        attack = 0;
        forward = 1;
        spriteRenderer.flipX = false;
        gameObject.SetActive(false);
    }
}
